package com.szd.qiongyou20.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.szd.qiongyou20.R;
import com.szd.qiongyou20.home.activity.ShowActivity;
import com.szd.qiongyou20.home.bean.HomeListData;

import java.util.List;

/**
 * Created by SZD on 2016/12/6.
 */

public class ViewPager_Home_Adapter extends PagerAdapter {

    private Context mContext;
    private List<HomeListData.DataBean.SlideBean> mList;
    private LayoutInflater mInflater;
    private int mSize;

    public ViewPager_Home_Adapter(Context context, List<HomeListData.DataBean.SlideBean> list) {
        mContext = context;
        mList = list;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mSize = mList.size();
    }

    @Override
    public int getCount() {
        return 10000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = mInflater.inflate(R.layout.item_home_viewpager, container, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView_viewPager_item);

        String photo = mList.get(position%mSize).getPhoto();
        Glide.with(mContext).load(photo).into(imageView);

        // 获取点击事件的链接地址
        final String url = mList.get(position%mSize).getUrl();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShowActivity.class);
                intent.putExtra("url", url);
                mContext.startActivity(intent);
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }
}
